Editor.File = {
	newDiagram: function () {
		Editor.STATE.SHAPES.forEach(function(pair) {
			var shape = pair.value;
			if (!shape.isDeleted()) {
					shape._remove();
			}
			delete shape;
		});
		Editor.STATE.SHAPES.clear();
		delete Editor.STATE.CMDSTACK;
		Editor.STATE.CMDSTACK = new JS.Command.Stack();
	},
	exportDiagram: function () {
		$('#dialogExport').jqm({overlay: 40, modal: true}).jqmShow();
		$('#textExport').focus();
		$('#textExport').val(Editor.File.toJson());
	},
	closeExport: function () {
		$('#dialogExport').jqmHide();
	},
	prepareImportDiagram: function () {
		$('#dialogImport').jqm({overlay: 40, modal: true}).jqmShow();
		$('#textImport').focus();
	},
	textImportClear: function () {	
		$('#textImport').val('');
	},
	closeImport: function () {
		$('#dialogImport').jqmHide(); 
	},
	toJson: function () {
		var arrObjects = new Array();
		Editor.STATE.SHAPES.forEach(function(pair) {
			var shape = pair.value;
			if (!shape.isDeleted()) {
					arrObjects.push(shape.toObject());
			}
		});
		var str = JSON.stringify(arrObjects);
		delete arrObjects;
		return str;
	},
	importDiagram: function () {
		var i;
		var obj;
		var arrObjects;
		Editor.File.newDiagram();	
		if ( $('#typeImportJson').attr('checked') ) {
			arrObjects = JSON.parse($('#textImport').val());	
		} else if ( $('#typeImportXml').attr('checked') ) {
			arrObjects = Editor.File.RdfParse($('#textImport').val());
		}
		Editor.Util.drawObjects(arrObjects);
		Editor.STATE.SHAPES.forEach(function(pair) {
			var shape = pair.value;
			if (!shape.isDeleted()) {	
					if (shape.getTypeClass() == "resource") {
						if (shape.getName().indexOf("genid:") == 0) {
							shape.setName(" ");
						}
					}
					shape.updateText();
			}
		});
		$('#dialogImport').jqmHide();
		delete arrObjects;
	},
	RdfParse: function(str) {
		var arrObjects = new Array();
		var rdf=new RDF();
		rdf.loadRDFXML(str);
		var arrNs = rdf.getArrPrefix();
		var arrTriples = rdf.getTriples();
		var toNs = function(res) {
			for (var j=0;j<arrNs.length;j++) {
				var prefix = arrNs[j];
				var uri = rdf.getUriNs(prefix);
				var pos = res.indexOf(uri);
				if (pos == 0) {
					return {'name':res.substr(uri.length),'namespace':{'prefix': prefix,'uri':uri}};
				}
			}
			return {'name':res,namespace:{'prefix':'',uri:''}};
		}
		var uuids = [];
		for (var i=0;i<arrTriples.length;i++) {
			var t = arrTriples[i];
			var x;
			var y;
			var w;
			var h;
			var uuid;
			var uuidIni;
			var uuidEnd;
			var name;
			var ns;
			w = Editor.CONFIG.RESOURCE_SHAPE_WIDTH;
			h = Editor.CONFIG.RESOURCE_SHAPE_HEIGHT;
			x = 2*w;
			y = (i+2)*(h+Editor.CONFIG.GRID_SIZE);
			uuid = Math.uuid();
			uuidIni = uuid;
			ns = toNs(t.subject);
			name = ns.name;
			if (uuids[name]) {
				uuidIni = uuids[name];
			} else {
				arrObjects.push({"type":"resource",
					"resource":{
						"element":{"name":name,"namespace":{"prefix":ns.namespace.prefix,"uri":ns.namespace.uri}},
						"shape":{"bbox":{"x":x,"y":y,"width":w,"height":h},"uuid":uuid}}});
				uuids[name] = uuid;
			}
			x += 4*w;
			uuid = Math.uuid();
			uuidEnd = uuid;
			if (t.type == 'resource' ) {
				ns = toNs(t.object);
				name = ns.name;
				if (uuids[name]) {
					uuidEnd = uuids[name];
				} else {
					arrObjects.push({"type":"resource",
						"resource":{
						"element":{"name":name,"namespace":{"prefix":ns.namespace.prefix,"uri":ns.namespace.uri}},
						"shape":{"bbox":{"x":x,"y":y,"width":w,"height":h},"uuid":uuid}}});
					uuids[name] = uuid;
				}
			} else if (t.type == 'literal') {
				text = t.object;
				if (uuids[text]) {
					uuidEnd = uuids[text];
				} else {
					arrObjects.push({"type":"literal",
						"text":text,"lang":t.lang,"datatype":t.datatype,
						"shape":{"bbox":{"x":x,"y":y,"width":w,"height":h},"uuid":uuid}});
					uuids[text] = uuid;
				}
			}
			x -= 2*w;
			uuid = Math.uuid();
			ns = toNs(t.predicate);
			name = ns.name;
			arrObjects.push({"type":"property","uuidIni":uuidIni,"uuidEnd":uuidEnd,
				"resource":{
					"element":{"name":name,"namespace":{"prefix":ns.namespace.prefix,"uri":ns.namespace.uri}},
					"shape":{"bbox":{"x":x,"y":y,"width":w,"height":h},"uuid":uuid}}});
		}
		return arrObjects;
	},
}
